package com.ujiuye.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        //获取执行方法名findAllStudent
        String method =req.getParameter("method");
        //获取BaseServlet以及其子类的字节码对象clazz-->StudentServlet类字节码对象
        Class<? extends BaseServlet> clazz=this.getClass();
            //根据方法名或方法的参数获取要执行的方法对象
        try {
            Method fa = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法
            fa.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
